<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Review & Rating Synthesis
 * @version 	1.0.2
 * @author 		Rukan <rukanhitokiri@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://www.zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

include_once(dirname(__FILE__).'/classes/ZCustomerBadges.php');
include_once(dirname(__FILE__).'/classes/ZCustomerList.php');

class AdminZCustomerBadges extends AdminTab
{
	/**
	 * @var string table name
	 */
	public $tableCustomer = 'customer';
	
	/** 
	 * @var object ProductList instance  
	 */
	private $_customerList;
	
	public function __construct()
	{
		global $cookie;
	 	$this->table = 'zcustomer_badges';
	 	$this->className = 'ZCustomerBadges';
	 	$this->lang = true;
	 	$this->edit = true;
		$this->delete = true;
	
		$this->fieldsDisplay = array(
			'id_zcustomer_badges'      => array('width' => 60,  'title' => $this->l('ID'), 'align' => 'center'),
			'name' 	  					 => array('width' => 250, 'title' => $this->l('Badges')));
	 	
		$this->_customerList = new ZCustomerList();
		
		parent::__construct();
	}
	
    /**
	 * Manage page processing
	 * @view AdminTab::postProcess()
	 *
	 * @global string $currentIndex Current URL in order to keep current Tab
	 */
	public function postProcess()
	{
		global $currentIndex, $cookie;
		$object = new $this->className();
		// set token
		$token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
		
		/* Cancel all filters for this tab */
		if (isset($_POST['submitReset'.$this->tableCustomer])) {
			if(isset($_POST['id_zcustomer_badges']))
				$id_zcustomer_badges = $_POST['id_zcustomer_badges'];
			$this->_customerList->postProcess();
			
			if (isset($id_zcustomer_badges)) {
				Tools::redirectAdmin($currentIndex.'&id_zcustomer_badges='.(int)$id_zcustomer_badges.'&updatezcustomer_badges&submit_reset&token='.$token);
			} else {
				Tools::redirectAdmin($currentIndex.'&addzcustomer_badges&submit_reset&token='.$token);
			}
		} 
		/* Manage list filtering */
		elseif (Tools::isSubmit('submitFilter'.$this->tableCustomer) OR $cookie->{'submitFilter'.$this->tableCustomer} !== false) {
			if(isset($_POST['pagination'])) {
				$cookie->{$this->tableCustomer.'_pagination'} = Tools::getValue('pagination');
			}
			$this->_customerList->postProcess();
			if (Tools::isSubmit('submitFilter'.$this->tableCustomer) && isset($_POST['submitFilter'])) {
				if (isset($_POST['id_zcustomer_badges'])) {
					Tools::redirectAdmin($currentIndex.'&id_zcustomer_badges='.(int)Tools::getValue('id_zcustomer_badges').'&updatezcustomer_badges&submit_filter&token='.$token);
				} else {
					Tools::redirectAdmin($currentIndex.'&addzcustomer_badges&submit_filter&token='.$token);
				}
			}
		}
		
		if (Tools::getValue('submitAdd'.$this->table) AND !isset($_POST['x']) AND !isset($_POST['y'])) {
				//add list customer badges
				if (($badgesCustomer = Tools::getValue('customerBox')) !== false OR (empty($badgesCustomer) AND is_array($badgesCustomer))){
					$badges = implode(",", $badgesCustomer);
					$_POST['id_zcustomer'] = $badges;
				}else{
					$this->_errors[] = Tools::displayError('Please select a or multiple customers.');
				}
		}
		
		
		if (!isset($_POST['x']) AND !isset($_POST['y'])) {
			parent::postProcess();
		}
	}
	
	/**
	 * Display form
	 * @view AdminTab::displayForm()
	 *
	 * @global string $currentIndex Current URL in order to keep current Tab
	 */
	public function displayForm($isMainTab = true)
	{
		global $currentIndex, $cookie;
		parent::displayForm();
		
		if (!($obj = $this->loadObject(true)))
			return;
			
		echo 
		'<form id="formbadges" action="'.$currentIndex.'&submitAdd'.$this->table.'=1&token='.$this->token.'" method="post">
		'.($obj->id ? '<input type="hidden" name="id_'.$this->table.'" value="'.$obj->id.'" />' : '').'
			<fieldset><legend><img src="../img/admin/tab-customers.gif" />'.$this->l('Add a new Badges Customer').'</legend>
				<label>'.$this->l('Name:').'</label>
				<div class="margin-form translatable">';
		foreach ($this->_languages as $language)
			echo 	'<div id="name_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $this->_defaultFormLanguage ? 'block' : 'none').'; float: left;">
						<input type="text" style="width: 260px" name="name_'.$language['id_lang'].'" id="name_'.$language['id_lang'].'" value="'.htmlentities($this->getFieldValue($obj, 'name', (int)($language['id_lang'])), ENT_COMPAT, 'UTF-8').'" /><sup> *</sup>
						<span class="hint" name="help_box">'.$this->l('Invalid characters:').' <>;=#{}<span class="hint-pointer">&nbsp;</span></span>
					</div>';
					$this->displayFlags($this->_languages, $this->_defaultFormLanguage, 'name', 'name');
		echo 		'<p class="clear">'.$this->l('').'</p>
				</div>';
		// List customer
		echo '<div id="customer_list">';
			$this->_customerList->arrayCustomer = $this->getFieldValue($obj, 'id_zcustomer');
			$this->_customerList->display($this->token);
		echo '</div>';
		echo '<div class="clear">&nbsp;</div>
			 <div class="margin-form">
					<input type="submit" value="'.(Tools::getValue('id_zcustomer_badges') ? $this->l('Modify this Badges') : $this->l('Add this Badges')).'" name="submitAdd'.$this->table.'" class="button" />
				</div>
				<div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
			</fieldset>
		</form>
		<p class="clear"></p>';
	}
	
}
